package com.zryc.shop.ums.controller;

import com.zryc.shop.base.Result;
import com.zryc.shop.ums.entity.Admin;
import com.zryc.shop.ums.entity.Menu;
import com.zryc.shop.ums.service.IMenuService;
import com.zryc.shop.ums.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-16
 */
@RestController
@Api(tags = "菜单管理接口列表")
@RequestMapping("/ums/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;


    @PostMapping("/delete/{id}")
    @ApiOperation("删除菜单")
    public Result delete(@PathVariable @ApiParam(value = "菜单id", required = true) Long id) {
        return Result.ok(menuService.deleteMenuById(id));
    }

    @PostMapping("/create")
    @ApiOperation("新增菜单")
    public Result create(@RequestBody Menu menu) {
        return Result.ok(menuService.save(menu));
    }


    @PostMapping("/update/{id}")
    @ApiOperation("更新菜单信息")
    public Result update(@PathVariable @ApiParam(value = "菜单id", required = true) Long id, @RequestBody Menu menu) {
        return Result.ok(menuService.updateMenu(id, menu));
    }

    @GetMapping("/{parentId}")
    @ApiOperation("获取当前菜单及其子菜单信息")
    public Result selectAllMenu(@PathVariable @ApiParam(value = "菜单id", required = true) Long parentId) {
        return Result.ok(menuService.getById(parentId));
    }

    @GetMapping("/list/{parentId}")
    @ApiOperation("分页查询全部菜单列表")
    public Result list(@PathVariable @ApiParam(value = "父id") Integer parentId,
                       @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                       @RequestParam(defaultValue = "10") @ApiParam(value = "每页大小", required = true) Integer pageSize) {
        return Result.ok(menuService.list(parentId, pageNum, pageSize));
    }

    @GetMapping("/treeList")
    @ApiOperation("分配菜单树")
    public Result treeList() {
        List<Menu> menuList = menuService.list();
        return Result.ok(menuService.buildtree(menuList));
    }

    @PostMapping("/updateHidden/{id}")
    @ApiOperation("更新菜单状态")
    public Result updateHidden(@PathVariable @ApiParam(value = "菜单id", required = true) Long id, @RequestParam Integer hidden) {
        return Result.ok(menuService.updateMenuHidden(id, hidden));
    }


}
